package cn.wzut.edut.dao.inf;

import cn.wzut.edut.dao.SnowflakeIdWorker;
import cn.wzut.edut.dao.mapper.PtyNoticeMapper;
import cn.wzut.edut.dao.mapper.PtyNoticeStuGroupRelMapper;
import cn.wzut.edut.dao.pojo.table.PtyClass;
import cn.wzut.edut.dao.pojo.table.PtyNotice;
import cn.wzut.edut.dao.pojo.table.PtyNoticeStuGroupRel;
import cn.wzut.edut.dao.pojo.table.PtyStu;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 公告操作
 */
@Repository
public class Notice {
    @Autowired
    private PtyNoticeMapper ptyNoticeMapper;
    @Autowired
    private PtyNoticeStuGroupRelMapper ptyNoticeStuGroupRelMapper;
    /**
     * 查询全部公告数量
     */
    public long countAllNotice(){return ptyNoticeMapper.selectCount(null);}
    /**
     * 查询全部公告数量以教师id
     */
    public long countAllNoticeInTeacher(String teacher_id){
        QueryWrapper<PtyNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("teacher_id",teacher_id);
        return ptyNoticeMapper.selectCount(queryWrapper);
    }
    /**
     * 查询全部公告
     */
    public List<PtyNotice> selectAllNotice(){return ptyNoticeMapper.selectList(null);    }
    /**
     * 查询全部公告以教师id
     */
    public List<PtyNotice> selectNoticeInTeacher(String teacher_id){
        QueryWrapper<PtyNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("teacher_id",teacher_id);
        return ptyNoticeMapper.selectList(queryWrapper);
    }

    /**
     * 查询公告以公告id
     */
    public List<PtyNotice> selectNoticeInId(String id){
        QueryWrapper<PtyNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",id);
        return ptyNoticeMapper.selectList(queryWrapper);
    }
    /**
     * 查询公告学生组表以公告id
     */
    public List<PtyNoticeStuGroupRel> selectNoticeRelInNotice(String notice_id){
        QueryWrapper<PtyNoticeStuGroupRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("notice_id",notice_id);
        return ptyNoticeStuGroupRelMapper.selectList(queryWrapper);
    }
    /**
     * 查询公告学生组表以学生组id
     */
    public List<PtyNoticeStuGroupRel> selectNoticeRelInGroup(String group_id){
        QueryWrapper<PtyNoticeStuGroupRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("stu_group_id",group_id);
        return ptyNoticeStuGroupRelMapper.selectList(queryWrapper);
    }
    /**
     * 添加公告
     */
    public String insertPtyNotice(PtyNotice ptyNotice){
        try {
            SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
            ptyNotice.setId(String.valueOf(snowflakeIdWorker.nextId()));
            ptyNoticeMapper.insert(ptyNotice);
        }catch (Exception e){
            return ptyNotice.getId();
        }
        return ptyNotice.getId();
    }
    /**
     * 添加公告学生组表
     */
    public String insertPtyNoticeRel(PtyNoticeStuGroupRel ptyNoticeStuGroupRel){
        try {
            SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
            ptyNoticeStuGroupRel.setId(String.valueOf(snowflakeIdWorker.nextId()));
            ptyNoticeStuGroupRelMapper.insert(ptyNoticeStuGroupRel);
        }catch (Exception e){
            return ptyNoticeStuGroupRel.getId();
        }
        return ptyNoticeStuGroupRel.getId();
    }
    /**
     * 更新公告
     */
    public boolean updatePtyNotice(PtyNotice ptyNotice){
        try {
            QueryWrapper<PtyNotice> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",ptyNotice.getId());
            ptyNoticeMapper.update(ptyNotice,queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 删除公告以公告id
     */
    public boolean delPtyNotice(String id){
        try {
            delPtyNoticeRelInNotice(id);
            QueryWrapper<PtyNotice> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",id);
            ptyNoticeMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 删除公告以教师id
     */
    public boolean delPtyNoticeInTeacher(String teacher_id){
        try {
            QueryWrapper<PtyNotice> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("teacher_id",teacher_id);
            List<PtyNotice> list = ptyNoticeMapper.selectList(queryWrapper);
            for (PtyNotice ptyNotice : list) {
                delPtyNoticeRelInNotice(ptyNotice.getId());
            }
            ptyNoticeMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 删除公告学生组表以公告id
     */
    public boolean delPtyNoticeRelInNotice(String notice_id){
        try {
            QueryWrapper<PtyNoticeStuGroupRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("notice_id",notice_id);
            ptyNoticeStuGroupRelMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 删除公告学生组表以学生组id
     */
    public boolean delPtyNoticeRelInGroup(String group_id){
        try {
            QueryWrapper<PtyNoticeStuGroupRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("stu_group_id",group_id);
            ptyNoticeStuGroupRelMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 删除公告学生组表以公告id和学生组id
     */
    public boolean delPtyNoticeRel(String notice_id,String group_id){
        try {
            QueryWrapper<PtyNoticeStuGroupRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("notice_id",notice_id).in("stu_group_id",group_id);
            ptyNoticeStuGroupRelMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
}
